package com.syb.controller.function;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.syb.entity.function.SysResource;
import com.syb.service.LoginService;

@SuppressWarnings("serial")
@Namespace("/sys")
public class LoginController extends ActionSupport{
	private static  Logger logger  = Logger.getLogger(LoginController.class);
	
	private String name;
	
	private String password;

	@Autowired
	private LoginService loginService;
	
	@Action(value="login",
			results={
			@Result(name="success",location="/jsp/background/framework/main.jsp"),
			@Result(name="error",location="/jsp/background/framework/login.jsp")
			})
	public String login(){
		ActionContext context=ActionContext.getContext();
		Map<String,Object> map = new HashMap<String,Object>();
		//鐖惰妭鐐�
		List<SysResource> resource = new ArrayList<SysResource>();
		//瀛愯妭鐐�
		List<SysResource> parent = new ArrayList<SysResource>();
		List<Object> resourceAll = new ArrayList<Object>();
			try {
				map = loginService.findSysUser(name, password);
			} catch (Exception e) {
				logger.error(e.getMessage());
				context.put("error", "验证用户失败！");
				return "error";
			}
			if(map==null){
				context.put("error", "用户名或者密码失败！");
				return "error";
			}
			ActionContext.getContext().getSession().put("userSession", map.get("sysUser"));
			ActionContext.getContext().getSession().put("sysRole", map.get("sysRole"));
			List<SysResource> sysResource = (List<SysResource>)map.get("sysResource");
			//鐖惰妭鐐�
			if(sysResource!=null && sysResource.size()>0){
				for(int i=0;i<sysResource.size();i++){
					
					 if(sysResource.get(i).getSysResource().size()>0){
						 resource.add(sysResource.get(i));
						 for(int k=0;k<sysResource.get(i).getSysResource().size();k++){
							 parent.add(sysResource.get(i).getSysResource().get(k));
						 }
					}else{
						resource.add(sysResource.get(i));
					}
				}
			}
			for(int i=0;i<parent.size();i++){
				resource.remove(parent.get(i));
			}
			
			//瀛愯妭鐐�
			if(resource.size()>0){
				for(int i=0;i<resource.size();i++){
					Map<String,Object> m = new HashMap<String,Object>();
					m.put("id", resource.get(i).getId());
					m.put("resourceName", resource.get(i).getResourceName());
					m.put("resourceUrl", resource.get(i).getResourceUrl());
					List<SysResource> o = new ArrayList<SysResource>();
					for(int k=0;k<resource.get(i).getSysResource().size();k++){
						for(int j=0;j<parent.size();j++){
							if(resource.get(i).getSysResource().get(k).getId().equals(parent.get(j).getId())){
								o.add(parent.get(j));
							}
						}
					}
					m.put("parent", o);
					resourceAll.add(m);
				}
			}
			ActionContext.getContext().getSession().put("resources", resourceAll);
			return "success";
	}
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	
	
}
